Skip to content

Introduce IOCP based AsyncIO for Windows #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

iCharlesHu
Copy link
Contributor

This PR finishes AsyncIO by introducing Windows IO Completion Port based implementation.

This PR is staged on top of #64 and should be considered ready to be review as soon as that one lands.

- Darwin: based on DispatchIO
- Linux: based on epoll
- Windows (not included in this commit): based on IOCP with OVERLAPPED
- Darwin: based on DispatchIO
- Linux: based on epoll
- Windows (not included in this commit): based on IOCP with OVERLAPPED
@iCharlesHu iCharlesHu requested review from parkera and itingliu July 7, 2025 20:23
@iCharlesHu
Copy link
Contributor Author

Resolves: #110

@itingliu
Copy link
Contributor

itingliu commented Jul 7, 2025

Talked to @iCharlesHu offline and he confirmed that if we merge this one, we won't need #64

// if another part of the code inadvertently reuses the same file descriptor
// number. This problem is especially concerning on Unix systems due to POSIX’s
// guarantee of using the lowest available file descriptor number, making reuse
// more probable. We use `fatalError` upon receiving `.badFileDescriptor`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this comment still refers to the non-Windows path

internal init(closeWhenDone: Bool, purpose: Purpose) throws {
#if canImport(WinSDK)
// On Windows, we need to create a named pipe
let pipeName = "\\\\.\\pipe\\subprocess-\(purpose)-\(Int.random(in: .min ..< .max))"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be a bit worried about running into collisions here, there is a good chance this could repeat and lead to subtle bugs. Instead of making it random, what about using a monotonically increasing unsigned 64-bit integer stored in a static variable? Then we can guarantee this is never a problem.

Something like this maybe?

func nextPipeNumber() -> UInt64 {
    struct Static: ~Copyable {
        static let pipeNumber = Atomic<UInt64>(0)
    }
    return Static.pipeNumber.add(1, ordering: .sequentiallyConsistent).newValue
}

nil
)
// Wait for monitor thread to exit
WaitForSingleObject(monitorThreadHandle, INFINITE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: stray semicolon

// Old registration found. This means this handle has
// already been registered. We simply need to update
// the continuation saved
storage[completionKey] = continuation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there scenarios where this is expected, and couldn't this leave continuations that never get resumed? Should this fatalError?

// Get a pointer to the memory at the specified offset
// Windows ReadFile uses DWORD for target count, which means we can only
// read up to DWORD (aka UInt32) max.
let targetCount = min(bufferPointer.count - readLength, Int(UInt32.max))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift also supports x86 (32-bit) Windows, Int(UInt32.max) will overflow and crash because Int is Int32 and not Int64 there.

// Windows WriteFile uses DWORD for target count
// which means we can only write up to DWORD max
let remainingLength = min(
ptr.count - writtenLength, Int(DWORD.max)
Copy link
Contributor

@jakepetroules jakepetroules Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int(DWORD.max) will also crash on x86-32. There's a few more occurrences of this later in the file as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants